package com.cssy.authority.web;


import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.byd.acegiext.domain.Role;
import com.byd.acegiext.service.RoleService;
import com.byd.acegiext.service.impl.RoleServiceImpl;
import com.cssy.authority.entity.ScUserRolewhPlaceDO;
import com.cssy.authority.service.IScUserRolewhPlaceService;
import com.cssy.base.web.BodyMsg;
import com.cssy.xxkj.basic.entity.BasicStorageInfoTestDO;
import com.cssy.xxkj.basic.entity.BasicWorkshopDO;
import com.cssy.xxkj.basic.service.IBasicStorageInfoTestService;
import com.cssy.xxkj.basic.service.IBasicWorkshopService;
import com.plat.helper.JqGridHandler;
import com.plat.helper.SearchCondition;
import com.plat.helper.SearchConditions;
import com.plat.helper.SearchResult;
import com.plat.util.GlobalResLoader;
import com.plat.util.SessionUtil;
import com.plat.util.json.AjaxUtil;
import com.plat.util.json.OutJsonGrid;

@Controller
@RequestMapping("/security/roleplace")
public class ScUserRolewhPlaceController {
	private static final String MANAGER = "/security/roleWhPlace/Manager";//列表页面
	private static final String EIDT = "/security/roleWhPlace/Operation";//添加、修改、显示页面
	private static final String SHEIDT = "/security/roleWhPlace/SHOperation";//添加、修改、显示页面
	
	@Resource(name="scUserRolePlaceService")
	private IScUserRolewhPlaceService scUserRolePlaceService;
		
	//仓库管理Service
	@Resource(name = "basicStorageInfoTestService")
	private IBasicStorageInfoTestService basicStorageInfoTestService;
	
	//车间管理Service
	@Resource(name = "basicWorkshopService")
	private IBasicWorkshopService basicWorkshopService;
	
	@RequestMapping("/manager")
	public String manager(HttpServletRequest request, Model model)
			throws Exception {
		String roleType = request.getParameter("roleType");
		model.addAttribute("roleType", roleType);
		return MANAGER;
	}
	
	
	@RequestMapping("/list")
	public void list(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String roleType = request.getParameter("roleType");
		JqGridHandler hander=new JqGridHandler(request);
		SearchConditions scs = new SearchConditions();
		hander.getWheres(scs);	
		scs.setRemovedProperty(null);
		String roleName=request.getParameter("ID");
		scs.addCondition("roleName", roleName,SearchCondition.OP_EQUAL);
		SearchResult result = null;
		if("wh".equals(roleType)){
			scs.addCondition("type", "WH", SearchCondition.OP_EQUAL);
			result = scUserRolePlaceService.findListByExample(scs);
		}
		if("sh".equals(roleType)){
			scs.addCondition("type", "SH", SearchCondition.OP_EQUAL);
			result = scUserRolePlaceService.findShopListByExample(scs);
		}
		if(result==null){
			result = new SearchResult();
		}
		OutJsonGrid out=new OutJsonGrid(result);
		out.printJSon(response);
		
	}

	@RequestMapping("/operation/{flag}/{id}")
	public String operationForm(HttpServletRequest request,@PathVariable String id,
			@PathVariable String flag, Model model) throws Exception {
		String roleType = request.getParameter("roleType");
		RoleService rs = new RoleServiceImpl();
		Role role=rs.getRoleById(id);
		model.addAttribute("role", role);
		if("wh".equals(roleType)){
			SearchConditions whScs = new SearchConditions();
			if(whScs.getOrderBy()==null||"".equals(whScs.getOrderBy())){
				 whScs.setOrderBy("zd003Name");
			}
			whScs.setRemovedProperty("removed");
			SearchResult st = basicStorageInfoTestService.findListByExample(whScs);
			List<BasicStorageInfoTestDO> bclist = st.getResults();
			model.addAttribute("bclist", bclist);
			return EIDT;
		}
		//车间
		if("sh".equals(roleType)){
			SearchConditions shScs = new SearchConditions();
			if(shScs.getOrderBy()==null||"".equals(shScs.getOrderBy())){
				shScs.setOrderBy("zd002Name");
			}
			shScs.setRemovedProperty("removed");
			SearchResult st = basicWorkshopService.findListByExample(shScs);
			List<BasicWorkshopDO> wslist = st.getResults();
			model.addAttribute("wslist", wslist);
			return SHEIDT;
		}
		return EIDT;
	}
	
	
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> save(HttpServletRequest request,
			ScUserRolewhPlaceDO curAP, BindingResult result, Model model)
	{
		BodyMsg bodyMap=BodyMsg.getInstance();
		
		try {
			if(curAP.getId()!=null && !"".equals(curAP.getId())){//修改
				scUserRolePlaceService.update(curAP, SessionUtil.getUser(request));
				bodyMap.isSuccess();
				bodyMap.configWebInfor(GlobalResLoader.getOpsuccess());
				bodyMap.configWebId(curAP.getId());
			}else{//新增
				ScUserRolewhPlaceDO cdo=scUserRolePlaceService.save(curAP, SessionUtil.getUser(request));
				bodyMap.isSuccess();
				bodyMap.configWebInfor(GlobalResLoader.getOpsuccess());
				bodyMap.configWebId(cdo.getId());
				
			}
			
		} catch (Exception e) {			
			e.printStackTrace();
			bodyMap.isFail();
			bodyMap.configWebInfor(GlobalResLoader.getOpfailed()+e.getMessage());
		}
		return bodyMap.modelMap;
		
	}
		

	@RequestMapping("/delete")
	@ResponseBody
	public Map<String, Object> delete(HttpServletRequest request,
			HttpServletResponse response, Model model) throws Exception {
		BodyMsg bodyMap=BodyMsg.getInstance();
		try{
			String ids = request.getParameter("ids");
			String[] fields=ids.split(",");
			for(String field : fields){
				scUserRolePlaceService.delete(field, SessionUtil.getUser(request));
			}
			bodyMap.isSuccess(GlobalResLoader.getOpsuccess());
		}catch(Exception e){
			e.printStackTrace();
			bodyMap.isFail(GlobalResLoader.getOpfailed()+e.getMessage());
		}
		return bodyMap.modelMap;
	}

	@RequestMapping(value = "/tree")
	public void treelist(HttpServletRequest request,HttpServletResponse response)throws Exception{
		RoleService rs = new RoleServiceImpl();
		int count = rs.countRolesByPartName(null, "1");
		List<Role> roles = rs.getRolesByPartName(null, "1", 1, 10000);
		String tr=null;
			StringBuffer sf =new StringBuffer();
			sf.append("[");
			sf.append("{");
			sf.append("\"text\":").append("\"").append("角色列表").append("\"");
			sf.append(",").append("\"children\":");
			sf.append("[");
			for(int i=0;i<roles.size();i++){
					sf.append("{");
					sf.append("\"text\":").append("\"").append(roles.get(i).getDescription()).append("\"").append(",").append("\"id\":").append("\"").append(roles.get(i).getId()).append("\"").append(",").append("\"name\":").append("\"").append(roles.get(i).getName()).append("\"");
					if(i==(roles.size()-1)){
						sf.append("}");
					}else{
						sf.append("},");
					}
					
			}
			sf.append("]");
			sf.append("}");
			sf.append("]");
			tr=sf.toString();
		AjaxUtil.printJSonArr(response, tr);
	}
}
